package com.bah.tract.client.ui.menu;

import java.awt.Image;
import java.awt.MenuItem;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;

import javax.inject.Named;
import javax.swing.JComboBox;
import javax.swing.JDialog;

import com.bah.tract.security.WindowsKeystore;
import com.bah.tract.security.X509PasswordEncryptor;
import com.google.inject.Inject;

/**
 * @author 524506
 */
public class PreferencesMenuItem extends MenuItem {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JDialog preferencesDialog;

	private final Preferences mPreferences;

	private final com.bah.tract.client.ui.PreferencePanel mTolPanel;
	private final com.bah.tract.client.ui.PreferencePanel mJiraPanel;

	@Inject
	PreferencesMenuItem(
			final Preferences preferences,
			@Named("tolPreferences") final com.bah.tract.client.ui.PreferencePanel tolPanel,
			@Named("jiraPreferences") final com.bah.tract.client.ui.PreferencePanel jiraPanel) {
		super("Preferences");
		mTolPanel = tolPanel;
		mJiraPanel = jiraPanel;
		mPreferences = preferences;
		addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(final ActionEvent e) {
				preferencesDialog = new JDialog();
				preferencesDialog.setTitle("TRACT - Preferences");
				preferencesDialog.setResizable(false);
				final Image image = Toolkit.getDefaultToolkit().getImage(
						"stopwatch.PNG");
				preferencesDialog.setIconImage(image);
				preferencesDialog.getContentPane().add(new PreferencePanel());
				preferencesDialog.pack();
				preferencesDialog.setVisible(true);
			}
		});
	}

	/**
	 * 
	 * @author 524506
	 */
	private class PreferencePanel extends javax.swing.JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private JComboBox clientCertBox;

		/** Creates new form NewJPanel1 */
		public PreferencePanel() {
			initComponents();
		}

		private void initComponents() {
			final String[] possibilities = WindowsKeystore.getAliases();
			final String alias = mPreferences.get(
					X509PasswordEncryptor.CERTIFICATE_PROP, possibilities[0]);
			final int reminderInterval = mPreferences.getInt("reminder", 15);
			clientCertLbl = new javax.swing.JLabel();
			reminderLbl = new javax.swing.JLabel();
			reminder = new javax.swing.JSpinner();
			minutesLbl = new javax.swing.JLabel();
			clientCertBox = new javax.swing.JComboBox();
			cancelBtn = new javax.swing.JButton();
			okBtn = new javax.swing.JButton();
			setName("Form"); // NOI18N

			final ResourceBundle resourceMap = ResourceBundle
					.getBundle("com.bah.tract.client.ui.PreferencesMenu");
			clientCertLbl.setText(resourceMap.getString("jLabel1.text")); // NOI18N
			clientCertLbl.setName("jLabel1"); // NOI18N

			reminderLbl.setText(resourceMap.getString("jLabel2.text")); // NOI18N
			reminderLbl.setName("jLabel2"); // NOI18N

			reminder.setName("jSpinner1"); // NOI18N
			reminder.setValue(reminderInterval);

			minutesLbl.setText(resourceMap.getString("jLabel3.text")); // NOI18N
			minutesLbl.setName("jLabel3"); // NOI18N

			clientCertBox.setModel(new javax.swing.DefaultComboBoxModel(
					possibilities));
			clientCertBox.setSelectedItem(alias);
			clientCertBox.setName("jComboBox1"); // NOI18N

			cancelBtn.setText(resourceMap.getString("jButton1.text")); // NOI18N
			cancelBtn.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(final ActionEvent e) {
					preferencesDialog.setVisible(false);
				}
			});
			cancelBtn.setName("jButton1"); // NOI18N

			okBtn.setText(resourceMap.getString("jButton2.text")); // NOI18N
			okBtn.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(final ActionEvent e) {
					final String cert = (String) clientCertBox.getModel()
							.getSelectedItem();
					mPreferences.put("certificate", cert);
					mPreferences.putInt("reminder",
							((Integer) reminder.getValue()).intValue());
					mTolPanel.savePreferences();
					mJiraPanel.savePreferences();
					preferencesDialog.setVisible(false);
				}
			});
			okBtn.setName("jButton2"); // NOI18N

			final javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
					this);
			setLayout(layout);
			layout.setHorizontalGroup(layout
					.createParallelGroup(
							javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(
							layout.createSequentialGroup()
									.addContainerGap()
									.addGroup(
											layout.createParallelGroup(
													javax.swing.GroupLayout.Alignment.LEADING)
													.addComponent(
															mTolPanel,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															Short.MAX_VALUE)
													.addComponent(
															mJiraPanel,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															Short.MAX_VALUE)
													.addGroup(
															layout.createSequentialGroup()
																	.addComponent(
																			clientCertLbl)
																	.addGap(41,
																			41,
																			41)
																	.addComponent(
																			clientCertBox,
																			0,
																			318,
																			Short.MAX_VALUE))
													.addGroup(
															javax.swing.GroupLayout.Alignment.TRAILING,
															layout.createSequentialGroup()
																	.addComponent(
																			okBtn)
																	.addPreferredGap(
																			javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																	.addComponent(
																			cancelBtn))
													.addGroup(
															layout.createSequentialGroup()
																	.addComponent(
																			reminderLbl)
																	.addPreferredGap(
																			javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																	.addComponent(
																			reminder,
																			javax.swing.GroupLayout.PREFERRED_SIZE,
																			43,
																			javax.swing.GroupLayout.PREFERRED_SIZE)
																	.addGap(10,
																			10,
																			10)
																	.addComponent(
																			minutesLbl)))
									.addContainerGap()));
			layout.setVerticalGroup(layout
					.createParallelGroup(
							javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(
							layout.createSequentialGroup()
									.addContainerGap()
									.addGroup(
											layout.createParallelGroup(
													javax.swing.GroupLayout.Alignment.BASELINE)
													.addComponent(clientCertLbl)
													.addComponent(
															clientCertBox,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															javax.swing.GroupLayout.PREFERRED_SIZE))
									.addGap(18, 18, 18)
									.addComponent(
											mTolPanel,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											javax.swing.GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(
											javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
									.addGap(18, 18, 18)
									.addComponent(
											mJiraPanel,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											javax.swing.GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(
											javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
									.addGroup(
											layout.createParallelGroup(
													javax.swing.GroupLayout.Alignment.BASELINE)
													.addComponent(reminderLbl)
													.addComponent(
															reminder,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															javax.swing.GroupLayout.PREFERRED_SIZE)
													.addComponent(minutesLbl))
									.addPreferredGap(
											javax.swing.LayoutStyle.ComponentPlacement.RELATED,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											Short.MAX_VALUE)
									.addGroup(
											layout.createParallelGroup(
													javax.swing.GroupLayout.Alignment.BASELINE)
													.addComponent(cancelBtn)
													.addComponent(okBtn))
									.addContainerGap()));
		}// </editor-fold>

		// Variables declaration - do not modify
		private javax.swing.JButton cancelBtn;
		private javax.swing.JButton okBtn;
		private javax.swing.JLabel clientCertLbl;
		private javax.swing.JLabel reminderLbl;
		private javax.swing.JLabel minutesLbl;
		private javax.swing.JSpinner reminder;
		// End of variables declaration

	}

}
